package org.xxpay.pay.channel;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.xxpay.core.common.constant.PayConstant;
import org.xxpay.core.common.vo.DBApplicationConfig;
import org.xxpay.pay.service.CommonService;

import java.io.File;

/**
 * @author: dingzhiwei
 * @date: 17/12/24
 * @description:
 */
@Component
public class BaseService {

    @Autowired
    public CommonService commonService;

    @Autowired
    public PayConfig payConfig;

    protected DBApplicationConfig getDBApplicationConfig(){
        return commonService.sysConfigService.getDBApplicationConfig();
    }

    protected String getNotifyUrl(String channelName, DBApplicationConfig cacheConfig) {
        if(cacheConfig == null) cacheConfig = getDBApplicationConfig();
        String notifyUrl = cacheConfig.getConfig4PayNotifyUrl();
        if(StringUtils.isBlank(channelName)) return notifyUrl;
        return String.format(notifyUrl, channelName);
    }

    protected String getReturnUrl(String channelName, DBApplicationConfig cacheConfig) {
        if(cacheConfig == null) cacheConfig = getDBApplicationConfig();
        String returnUrl = cacheConfig.getConfig4PayReturnUrl();
        if(StringUtils.isBlank(channelName)) return returnUrl;
        return String.format(returnUrl, channelName);
    }

    protected String getNotifyTransUrl(String channelName, DBApplicationConfig cacheConfig) {
        if(cacheConfig == null) cacheConfig = getDBApplicationConfig();
        String notifyTransUrl = cacheConfig.getConfig4PayNotifyTransUrl();
        if(StringUtils.isBlank(channelName)) return notifyTransUrl;
        return String.format(notifyTransUrl, channelName);
    }

    protected String getNotifyDivisionUrl(String channelName, DBApplicationConfig cacheConfig) {
        if(cacheConfig == null) cacheConfig = getDBApplicationConfig();
        String notifyDivisionUrl = cacheConfig.getConfig4PayNotifyDivisionUrl();
        if(StringUtils.isBlank(channelName)) return notifyDivisionUrl;
        return String.format(notifyDivisionUrl, channelName);
    }

    protected String getJumpUrl(DBApplicationConfig cacheConfig) {
        if(cacheConfig == null) cacheConfig = getDBApplicationConfig();
        return cacheConfig.getConfig4PayUrl() + "/jump.htm";
    }




    protected JSONObject buildRetObj() {
        JSONObject retObj = new JSONObject();
        retObj.put(PayConstant.RETURN_PARAM_RETCODE, PayConstant.RETURN_VALUE_SUCCESS);
        return retObj;
    }

    protected JSONObject buildFailRetObj() {
        JSONObject retObj = new JSONObject();
        retObj.put(PayConstant.RETURN_PARAM_RETCODE, PayConstant.RETURN_VALUE_FAIL);
        return retObj;
    }

    protected JSONObject buildRetObj(String retValue, String retMsg) {
        JSONObject retObj = new JSONObject();
        retObj.put(PayConstant.RETURN_PARAM_RETCODE, retValue);
        retObj.put(PayConstant.RETURN_PARAM_RETMSG, retMsg);
        return retObj;
    }

    /**
     * 获取证书文件路径
     * @param channelName
     * @param fileName
     * @return
     */
    public String getCertFilePath(String channelName, String certRootPath, String fileName) {
        return certRootPath + File.separator + channelName + File.separator + fileName;
    }

}
